import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import command.Invoqueur;

@SuppressWarnings("serial")
public class FenetreZoomPos  extends WindowFactory implements  KeyListener, ChangeListener, Observer{
	
	
	private JTextArea TextPosX;
	private JTextArea TextPosY;	
	private JSlider SlideZoom;
	
	private Invoqueur lInvoqueur = Invoqueur.getInstance();
	
	private int PosX;
	private int PosY;
	private double Zoom;
	
	private static FenetreZoomPos inst = null;

	public static FenetreZoomPos getInstance(){
		if(inst == null){
			inst = new FenetreZoomPos();
		}
		return inst;		
	}

	public FenetreZoomPos() {
		
		
		
		JPanel panel = new JPanel();
		panel.setSize(300, 150);
		panel.setPreferredSize(new Dimension(300, 150));
		panel.setLayout(null);
		this.setLocation(700, 200);
		this.setSize(270, 130);

		this.setContentPane(panel);
		
		JLabel PositionLabel = new JLabel();
		PositionLabel.setText("Position : ");
		PositionLabel.setSize(70, 20);
		PositionLabel.setPreferredSize(new Dimension(70, 20));		
		PositionLabel.setLocation(5, 10);
		
		JLabel ZoomLabel = new JLabel();
		ZoomLabel.setText("Zoom : ");
		ZoomLabel.setSize(70, 20);
		ZoomLabel.setPreferredSize(new Dimension(70, 20));		
		ZoomLabel.setLocation(5, 50);
			
		
		JLabel labelX = new JLabel();
		labelX.setText("X = ");
		labelX.setSize(25, 20);
		labelX.setPreferredSize(new Dimension(25, 20));		
		labelX.setLocation(80, 10);
		
		JLabel labelY = new JLabel();
		labelY.setText("Y = ");
		labelY.setSize(20, 20);
		labelY.setPreferredSize(new Dimension(25, 20));		
		labelY.setLocation(150, 10);
		
		TextPosX = new JTextArea();
		TextPosX.setText("0");
		TextPosX.setSize(33, 20);
		TextPosX.setPreferredSize(new Dimension(33 ,20));		
		TextPosX.setLocation(105, 10);
		TextPosX.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		
		TextPosY = new JTextArea();
		TextPosY.setText("0");
		TextPosY.setSize(33, 20);
		TextPosY.setPreferredSize(new Dimension(33, 20));		
		TextPosY.setLocation(175, 10);
		TextPosY.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		
		TextPosX.addKeyListener(this);
		TextPosY.addKeyListener(this);
		
		
		SlideZoom = new JSlider(JSlider.HORIZONTAL, -100, 100, 1);
		SlideZoom.setMajorTickSpacing(50);
		SlideZoom.setMinorTickSpacing(50);
		SlideZoom.setPaintTicks(true);
		SlideZoom.setPaintLabels(true);
		SlideZoom.setSize(200, 50);
		SlideZoom.setPreferredSize(new Dimension(80, 50));		
		SlideZoom.setLocation(50, 50);
		SlideZoom.setValue(0);
		SlideZoom.addChangeListener(this);
		
				
		this.add(labelX);
		this.add(labelY);
		this.add(ZoomLabel);
		this.add(PositionLabel);
		this.add(TextPosY);
		this.add(TextPosX);
		this.add(SlideZoom);
		this.setAlwaysOnTop(true);
		this.setResizable(false);
	}
	

	@Override
	public void keyPressed(KeyEvent k) {
		
		if(k.getKeyCode() == KeyEvent.VK_ENTER)
		{
			ConcreteCommandeModifPos lCommandeA = new ConcreteCommandeModifPos(RecepteurPanneauImage.getInstance());
			lCommandeA.setI((Integer.parseInt(TextPosX.getText())));
			lCommandeA.setJ((Integer.parseInt(TextPosY.getText())));
			lInvoqueur.setCommandeModifPos(lCommandeA);
			lInvoqueur.invoquerModifPos();
					
		}		
	}
	
	public void ajustePos(int newX, int newY){

		setPosX(newX);
		setPosY(newY);
		/*
		zoomPos.setX(PosX);
		zoomPos.setY(PosY);*/
		ConcreteCommandeModifPos lCommandeA = new ConcreteCommandeModifPos(RecepteurPanneauImage.getInstance());
		lCommandeA.setI(PosX);
		lCommandeA.setI(PosY);
		lInvoqueur.setCommandeModifPos(lCommandeA);
		lInvoqueur.invoquerModifPos();
	}

	public void ajusteZoom(double zoom) {

		Zoom = zoom;
		//zoomPos.setZoom(Zoom);		
		ConcreteCommandeModifZoom lCommandeB = new ConcreteCommandeModifZoom(RecepteurPanneauImage.getInstance());
		lCommandeB.setI(zoom);
		lInvoqueur.setCommandeModifZoom(lCommandeB);
		lInvoqueur.invoquerModifZoom();
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public int getPosX() {
		return PosX;
	}

	public void setPosX(int posX) {
		PosX = posX;
		TextPosX.setText(String.valueOf(posX));	
		
	}

	
	public int getPosY() {
		return PosY;
	}

	public void setPosY(int posY) {
		PosY = posY;
		TextPosY.setText(String.valueOf(posY));
		
	}


	public double getZoom() {
		return Zoom;
	}

	public void setZoom(double zoom) {
		Zoom = zoom;
		int test = (int) (-100 * (1 - zoom));
		SlideZoom.setValue(test);
	}



	@Override
	public void update(Observable o, Object arg) {
		
		ZoomPosition newZoomPosition = (ZoomPosition) arg;
		
		setPosX(newZoomPosition.getX());
		setPosY(newZoomPosition.getY());
		setZoom(newZoomPosition.getZoom());
		
	
		
	}

	@Override
	public void stateChanged(ChangeEvent e) {
		if(lInvoqueur.getSwithCom()==1){
			if(e.getSource() == SlideZoom) {
				if(SlideZoom.getValue() != 0) {

					ConcreteCommandeModifZoom lCommandeB = new ConcreteCommandeModifZoom(RecepteurPanneauImage.getInstance());
					lCommandeB.setI( ((double)SlideZoom.getValue() / 100) + 1 );
					lInvoqueur.setCommandeModifZoom(lCommandeB);
					lInvoqueur.invoquerModifZoom();

				}

				else {
					ConcreteCommandeModifZoom lCommandeB = new ConcreteCommandeModifZoom(RecepteurPanneauImage.getInstance());
					lCommandeB.setI( 1 );
					lInvoqueur.setCommandeModifZoom(lCommandeB);
					lInvoqueur.invoquerModifZoom();
				}

			}	
		}
		
	}

}
